package com.crossriver.crgis.cofo.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;

import javax.portlet.ActionRequest;
import javax.portlet.PortletSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.crossriver.crgis.database.model.Application_C_of_O;
import com.crossriver.crgis.database.model.Company_Contact_Person;
import com.crossriver.crgis.database.model.Held_Plots;
import com.crossriver.crgis.database.model.History_Of_Information;
import com.crossriver.crgis.database.model.Contact_Information;

import com.crossriver.crgis.database.model.Individual_Information;
import com.crossriver.crgis.database.model.Organisation_Information;
import com.crossriver.crgis.database.model.Property_Information;
import com.crossriver.crgis.database.model.Supporting_Document;


import com.crossriver.crgis.database.model.impl.Application_C_of_OImpl;
import com.crossriver.crgis.database.model.impl.Company_Contact_PersonImpl;
import com.crossriver.crgis.database.model.impl.Held_PlotsImpl;
import com.crossriver.crgis.database.model.impl.History_Of_InformationImpl;
import com.crossriver.crgis.database.model.impl.Contact_InformationImpl;
import com.crossriver.crgis.database.model.impl.Individual_InformationImpl;
import com.crossriver.crgis.database.model.impl.Organisation_InformationImpl;
import com.crossriver.crgis.database.model.impl.Property_InformationImpl;
import com.crossriver.crgis.database.model.impl.Supporting_DocumentImpl;
import com.crossriver.crgis.portlet.ApplicationCofO;
import com.liferay.portal.kernel.upload.UploadPortletRequest;
import com.liferay.portal.kernel.util.Base64;
import com.liferay.portal.kernel.util.CalendarFactoryUtil;
import com.liferay.portal.kernel.util.FileUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.service.UserServiceUtil;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;

/**
 * 
 * @author Wole Adetiba
 * 
 * This utility class is used for retrieving values submitted from the web.
 * This class acts as the central place for building the objects (domain) 
 * that can be validated and then used for further processing.
 * 
 * Apart from this, input cleaning exercise can be done here when the code 
 * is to be refined later.
 * 
 * Utility methods are called from the ApplicationCofO portlet.
 *
 */

public class ApplicationActionUtil {
	
	
	//logger
	private static Log log = LogFactory.getLog(ApplicationActionUtil.class);
	
	
	/**
	 * This methos is used for retrieving  Application_C_of_O information
	 * from the ActionRequest object from the portal.
	 * 
	 * @param request
	 * @return Applicatiob_C_of_O
	 */
	public static Application_C_of_O getApplication_C_of_OFromRequest(ActionRequest request){
		
		PortletSession portletSession = request.getPortletSession();
		Application_C_of_O applCofO  = (Application_C_of_O)
			portletSession.getAttribute(ApplicationCofOConstants.APPL_C_OF_O_SESSION_ATTR);
		
		if(applCofO == null){
			applCofO = new Application_C_of_OImpl();
		}
		
		ThemeDisplay themeDisplay = (ThemeDisplay) request
			.getAttribute(WebKeys.THEME_DISPLAY);
		
		
		
		applCofO.setApplicationType( ParamUtil.getString(request, "applicationType"));
		applCofO.setApplication( ParamUtil.getString(request, "application"));
		applCofO.setOldCofONo( ParamUtil.getString(request, "oldCofONo"));
		applCofO.setFileNo( ParamUtil.getString(request, "fileNo"));
		
		if(applCofO.getDateOfApplication() != null){
			//applCofO.setDateOfApplication(dateOfApplication)
		}
		applCofO.setUserId(new Long(themeDisplay.getUser().getUserId()).toString());
		applCofO.setStatus(ApplicationCofOConstants.APPL_C_OF_O_STATUS_NEW);
		applCofO.setApplicationId(ParamUtil.getString(request, "applicationId"));
		
		
		
		//Generate a new application Id
//		if(null == applCofO.getApplicationId() || "".equalsIgnoreCase(applCofO.getApplicationId())){
//			applCofO.setApplicationId(UUID.randomUUID().toString());
//			applCofO.setPrimaryKey(applCofO.getApplicationId());			
//		}
		
		applCofO.setLandUsePurpose(ParamUtil.getString(request, "purpose"));
		
		applCofO.setLandUseType(ParamUtil.getString(request, "useType"));
		
		applCofO.setAttestation(ParamUtil.getString(request, "attestation"));
		
		return applCofO;
	}
	
	/**
	 * This method is used for retrieving  Company_Contact_Person information
	 * from the ActionRequest object from the portal.
	 * 
	 * @param request
	 * @return Company_Contact_Person
	 */
	public static Company_Contact_Person getCompany_Contact_PersonFromRequest(ActionRequest request){
		
		PortletSession portletSession = request.getPortletSession();
		Company_Contact_Person compContPerson  = (Company_Contact_Person)
			portletSession.getAttribute(ApplicationCofOConstants.COY_CONTACT_SESSION_ATTR);
		
		if(compContPerson == null){
			compContPerson = new Company_Contact_PersonImpl();
		}
		
		ThemeDisplay themeDisplay = (ThemeDisplay) request
			.getAttribute(WebKeys.THEME_DISPLAY);
		
		compContPerson.setApplicationId(ParamUtil.getString(request, "applicationId"));
		compContPerson.setDesignation(ParamUtil.getString(request, "designation"));
		compContPerson.setEmailAddress(ParamUtil.getString(request, "emailAddress"));
		compContPerson.setFax(ParamUtil.getString(request, "fax"));
		compContPerson.setFirstName(ParamUtil.getString(request, "firstName"));
		compContPerson.setLastName(ParamUtil.getString(request, "lastName"));
		compContPerson.setMiddleName(ParamUtil.getString(request, "middleName"));
		compContPerson.setPhoneHome(ParamUtil.getString(request, "phoneHome"));
		compContPerson.setPhoneMobile(ParamUtil.getString(request, "phoneMobile"));
		compContPerson.setPhoneOffice(ParamUtil.getString(request, "phoneOffice"));
		compContPerson.setTitle(ParamUtil.getString(request, "title"));
		compContPerson.setIdentificationDoc(ParamUtil.getString(request, "identificationDocument"));
		compContPerson.setIdentificationDocNo(ParamUtil.getString(request, "identificationDocumentNo"));
		
		
		compContPerson.setUserId(new Long(themeDisplay.getUserId()).toString());
		
		return compContPerson;
	}
	
	/**
	 * This method is used for retrieving list of History_Of_Information information
	 * from the ActionRequest object from the portal.
	 * 
	 * @param request
	 * @return List of History_Of_Information
	 */
	public static List<History_Of_Information> getHistory_Of_InformationFromRequest(ActionRequest request){
		
		PortletSession portletSession = request.getPortletSession();
		Application_C_of_O applCofO  = (Application_C_of_O)
		portletSession.getAttribute(ApplicationCofOConstants.APPL_C_OF_O_SESSION_ATTR);
		
		
		
		
		
		List<History_Of_Information> histories = new ArrayList<History_Of_Information>();
		
		String[] applHistories = request.getParameterValues("applicationHistory");
		
		
		if(applHistories != null){
			for (String appHist : applHistories) {
				
				History_Of_Information history = new History_Of_InformationImpl();
				
				history.setOldApplicationId(appHist);
				if (applCofO != null){
					history.setApplicationId(applCofO.getApplicationId());
				}else{
					history.setApplicationId(ParamUtil.getString(request, "applicationId"));
					
				}
				
				history.setHistoryInfoId(UUID.randomUUID().toString());
				
				ThemeDisplay themeDisplay = (ThemeDisplay) request
				.getAttribute(WebKeys.THEME_DISPLAY);
				
				history.setUserId(new Long(themeDisplay.getUserId()).toString());
				
				System.out.println("Seen >>" + history.toString() );
				
				
				histories.add(history);
	        }			
		}

				
		
		return histories;
	}
	
	/**
	 * This method is used for retrieving Contact_Information information
	 * from the ActionRequest object from the portal.
	 * 
	 * @param request
	 * @return Contact_Information
	 */
	public static Contact_Information getContact_InformationFromRequest(ActionRequest request){
		
		PortletSession portletSession = request.getPortletSession();
		Contact_Information indContInfo  = (Contact_Information)
			portletSession.getAttribute(ApplicationCofOConstants.COY_CONTACT_SESSION_ATTR);
		
		if(indContInfo == null){
			indContInfo = new Contact_InformationImpl();
		}
		
		ThemeDisplay themeDisplay = (ThemeDisplay) request
			.getAttribute(WebKeys.THEME_DISPLAY);
		
		indContInfo.setApplicationId(ParamUtil.getString(request, "applicationId"));
		indContInfo.setAddressLine1(ParamUtil.getString(request, "addressLine1"));
		indContInfo.setAddressLine2(ParamUtil.getString(request, "addressLine2"));
		indContInfo.setCity(ParamUtil.getString(request, "city"));
		indContInfo.setCountry(ParamUtil.getString(request, "country"));
		indContInfo.setEmailAddress(ParamUtil.getString(request, "emailAddress"));
		indContInfo.setFax(ParamUtil.getString(request, "fax"));
		indContInfo.setHouseNo(ParamUtil.getString(request, "houseNo"));
		indContInfo.setState(ParamUtil.getString(request, "state"));
		indContInfo.setPhoneHome(ParamUtil.getString(request, "phoneHome"));
		indContInfo.setPhoneMobile(ParamUtil.getString(request, "phoneMobile"));
		indContInfo.setPhoneOffice(ParamUtil.getString(request, "phoneOffice"));
		indContInfo.setOtherInformation(ParamUtil.getString(request, "otherInfomation"));
		indContInfo.setPoBox(ParamUtil.getString(request, "postalMailAddress"));
		indContInfo.setLga(ParamUtil.getString(request, "lga"));

		indContInfo.setUserId(new Long(themeDisplay.getUserId()).toString());
		
		return indContInfo;
	}

	/**
	 * This method is used for retrieving Individual_Information information
	 * from the ActionRequest object from the portal.
	 * 
	 * @param request
	 * @return Individual_Information
	 */
	public static Individual_Information getIndividual_InformationFromRequest(ActionRequest request){
		
		PortletSession portletSession = request.getPortletSession();
		Individual_Information indInformation  = (Individual_Information)
			portletSession.getAttribute(ApplicationCofOConstants.INDIVIDUAL_INFO_SESSION_ATTR);
		
		if(indInformation == null){
			indInformation = new Individual_InformationImpl();
		}
		
		ThemeDisplay themeDisplay = (ThemeDisplay) request
			.getAttribute(WebKeys.THEME_DISPLAY);
		
		indInformation.setApplicationId(ParamUtil.getString(request, "applicationId"));
		indInformation.setUserId(new Long(themeDisplay.getUserId()).toString());
		
		Calendar cal = null;
		CalendarFactoryUtil.getCalendar(ParamUtil.getInteger(request, "dateOfBirthYear"), 
				ParamUtil.getInteger(request, "dateOfBirthMonth"), 
					ParamUtil.getInteger(request, "dateOfBirthDay"));
		
		if(ParamUtil.getInteger(request, "dateOfBirthYear") == 0 && 
				ParamUtil.getInteger(request, "dateOfBirthMonth") == 0 && 
				ParamUtil.getInteger(request, "dateOfBirthDay")== 0){
			
			cal = 	CalendarFactoryUtil.getCalendar();
			
		}else{
			cal = CalendarFactoryUtil.getCalendar(ParamUtil.getInteger(request, "dateOfBirthYear"), 
					ParamUtil.getInteger(request, "dateOfBirthMonth"), 
						ParamUtil.getInteger(request, "dateOfBirthDay"));
		}
		
		indInformation.setDateOfBirth(cal.getTime());
		indInformation.setEducation(ParamUtil.getString(request, "education"));
		indInformation.setFirstName(ParamUtil.getString(request, "firstName"));
		indInformation.setLastName(ParamUtil.getString(request, "lastName"));
		indInformation.setMiddleName(ParamUtil.getString(request, "middleName"));
		indInformation.setOccupation(ParamUtil.getString(request, "occupation"));
		indInformation.setGender(ParamUtil.getString(request, "gender"));
		indInformation.setReligion(ParamUtil.getString(request, "religion"));
		indInformation.setTitle(ParamUtil.getString(request, "title"));
		indInformation.setGender(ParamUtil.getString(request, "gender"));
		indInformation.setNationality(ParamUtil.getString(request, "nationality"));
		indInformation.setStateOfOrigin(ParamUtil.getString(request, "stateOfOrigin"));
		indInformation.setLga(ParamUtil.getString(request, "lga"));
		indInformation.setMaritalStatus(ParamUtil.getString(request, "maritalStatus"));
		indInformation.setNoOfChildren(ParamUtil.getInteger(request, "noOfChildren"));
		indInformation.setIdentificationDoc(ParamUtil.getString(request, "identificationDocument"));
		indInformation.setIdentificationDocNo(ParamUtil.getString(request, "identificationDocumentNo"));	
		
		return indInformation;
	}
	
	/**
	 * This method is used for retrieving Organisation_Information information
	 * from the ActionRequest object from the portal.
	 * 
	 * @param request
	 * @return Organisation_Information
	 */
	public static Organisation_Information getOrganisation_InformationFromRequestOLD(ActionRequest request){
		
		PortletSession portletSession = request.getPortletSession();
		Organisation_Information orgInformation  = (Organisation_Information)
			portletSession.getAttribute(ApplicationCofOConstants.ORGANISATION_INFO_SESSION_ATTR);
		
		if(orgInformation == null){
			orgInformation = new Organisation_InformationImpl();
		}
		
		ThemeDisplay themeDisplay = (ThemeDisplay) request
			.getAttribute(WebKeys.THEME_DISPLAY);
		
		orgInformation.setCountryOfReg(ParamUtil.getString(request, "countryOfReg"));
		
		Calendar cal = CalendarFactoryUtil.getCalendar(ParamUtil.getInteger(request, "regYear"), 
				ParamUtil.getInteger(request, "regMonth"), 
					ParamUtil.getInteger(request, "regDay"));
		orgInformation.setDateOfRegistration(cal.getTime());
		
		orgInformation.setOrganisationName(ParamUtil.getString(request, "organisationName"));
		orgInformation.setRegNo(ParamUtil.getString(request, "regNo"));
		orgInformation.setApplicationId(ParamUtil.getString(request, "applicationId"));
		orgInformation.setUserId(new Long(themeDisplay.getUser().getUserId()).toString());
		orgInformation.setCountryOfReg(ParamUtil.getString(request, "countryOfReg"));
		
		return orgInformation;
	}
	
	/**
	 * This method is used for retrieving Property_Information information
	 * from the ActionRequest object from the portal.
	 * 
	 * @param request
	 * @return Property_Information
	 */
	public static Property_Information getProperty_InformationFromRequest(ActionRequest request){
		
		PortletSession portletSession = request.getPortletSession();
		Property_Information propInformation  = (Property_Information)
			portletSession.getAttribute(ApplicationCofOConstants.PROPERTY_INFO_SESSION_ATTR);
		
		if(propInformation == null){
			propInformation = new Property_InformationImpl();
		}
		
		ThemeDisplay themeDisplay = (ThemeDisplay) request
			.getAttribute(WebKeys.THEME_DISPLAY);
		

		propInformation.setApplicationId(ParamUtil.getString(request, "applicationId"));

		propInformation.setAllocationMadToYou(ParamUtil.getString(request, "allocationMadeToYou"));
		propInformation.setArea(ParamUtil.getDouble(request, "area"));
		propInformation.setBlockNo(ParamUtil.getString(request, "blockNo"));
		propInformation.setCity(ParamUtil.getString(request, "city"));
		propInformation.setCountry(ParamUtil.getString(request, "country"));
		propInformation.setLandLocation(ParamUtil.getString(request, "landLocation"));
		propInformation.setOtherUsefulInformation(ParamUtil.getString(request, "otherUsefulInformation"));
		propInformation.setPropertyType(ParamUtil.getString(request, "propertyType"));
		
		propInformation.setLocationType(ParamUtil.getString(request, "locationType"));
		propInformation.setLga(ParamUtil.getString(request, "lga"));
		propInformation.setTown(ParamUtil.getString(request, "town"));
		propInformation.setVillage(ParamUtil.getString(request, "village"));
		
		if(propInformation.getVillage() == null || "".equalsIgnoreCase(propInformation.getVillage())){
			propInformation.setVillage(propInformation.getTown());
		}
		propInformation.setTerm(ParamUtil.getString(request, "term"));
		propInformation.setEstDevCost(ParamUtil.getString(request, "estDevCost"));
		
		propInformation.setPropertyType(ParamUtil.getString(request, "propertyType"));
		
		
		
		Calendar cal = null;
		
		if(ParamUtil.getInteger(request, "origCofODateYear") == 0 && 
				ParamUtil.getInteger(request, "origCofODateMonth") == 0 && 
				ParamUtil.getInteger(request, "origCofODateDay")== 0){
			
			cal = 	null; //CalendarFactoryUtil.getCalendar();
			
		}else{
			cal = CalendarFactoryUtil.
			     getCalendar(ParamUtil.getInteger(request, "origCofODateYear"), 
					ParamUtil.getInteger(request, "origCofODateMonth"), 
						ParamUtil.getInteger(request, "origCofODateDay"));
			
		}
		
		if(null != cal){
			propInformation.setOrigCofODate(cal.getTime());
		}else{
			propInformation.setOrigCofODate(null);
		}
		
		
		Calendar calState = null;
		
		if(ParamUtil.getInteger(request, "estStartYear") == 0 && 
				ParamUtil.getInteger(request, "estStartMonth") == 0 && 
				ParamUtil.getInteger(request, "estStartDay")== 0){
			
			calState = null; //	CalendarFactoryUtil.getCalendar();
			
		}else{
			calState = CalendarFactoryUtil.
			     getCalendar(ParamUtil.getInteger(request, "estStartYear"), 
					ParamUtil.getInteger(request, "estStartMonth"), 
					ParamUtil.getInteger(request, "estStartDay"));
			
		}
		
		
		if(null != calState){
			propInformation.setEstStartDate(null);
		}else{
			propInformation.setEstStartDate(null);
		}
		 
		
		
		propInformation.setPlotNo(ParamUtil.getString(request, "plotNo"));
		propInformation.setState( ParamUtil.getString(request, "state"));
		propInformation.setUserId(new Long(themeDisplay.getUserId()).toString());
		propInformation.setAssignedToYou(ParamUtil.getString(request, "assignedToYou"));
		propInformation.setUom(ParamUtil.getString(request, "unitOfMeasurement"));
		
		return propInformation;
	}

	public static List<Held_Plots>getHeldPlots(ActionRequest request){
		ThemeDisplay themeDisplay = (ThemeDisplay) request
		.getAttribute(WebKeys.THEME_DISPLAY);
	

	

		List <Held_Plots> heldPlots = new ArrayList<Held_Plots>();
				
		int intCounter = ParamUtil.getInteger(request, "counter");
		
		for (int i = 1; i <= intCounter; i++) {
			
			Held_Plots heldPlot = new Held_PlotsImpl();
			heldPlot.setHeld_id(UUID.randomUUID().toString());
			heldPlot.setApplicationId(ParamUtil.getString(request, "applicationId"));
			heldPlot.setUserId(new Long(themeDisplay.getUserId()).toString());
			heldPlot.setLocation(ParamUtil.getString(request,"location_" + i));
			heldPlot.setArea(ParamUtil.getString(request,"area_" + i));
			heldPlot.setDeveloped(ParamUtil.getString(request,"developed_" + i));
			heldPlot.setAcquisitionMode(ParamUtil.getString(request,"acquisition_" + i));
			heldPlot.setRegParticulars(ParamUtil.getString(request,"registration_" + i));
			
			heldPlots.add(heldPlot);
		}
		
		return heldPlots;
	}
		
	/**
	 * This method is used for retrieving List fo Supporting_Document information
	 * from the ActionRequest object from the portal.
	 * 
	 * @param request
	 * @return List Supporting_Document
	 */
	public static List<Supporting_Document> 
		getSupporting_DocumentFromRequest(ActionRequest request, final UploadPortletRequest uploadRequest){
				
		List<Supporting_Document> suppDocs = new ArrayList<Supporting_Document>();
		
		int intCounter = ParamUtil.getInteger(uploadRequest, "docsCounter");
		
		for (int i = 1; i <= intCounter; i++) {
			
			Supporting_Document supDoc = new Supporting_DocumentImpl();
			String paraName = "documentName_" + i;
			
			String fileName = uploadRequest.getFileName("fileName_" + i);
			
			supDoc.setDocumentName(ParamUtil.getString(uploadRequest, paraName));
			supDoc.setApplicationId(ParamUtil.getString(uploadRequest, "applicationId") );
			supDoc.setDocumentId(UUID.randomUUID().toString());
			supDoc.setRequired("YES");
			
			supDoc.setFileName(fileName);
			
			File file = uploadRequest.getFile("fileName_" + i);
			
			try {
				byte[] byteInFile = FileUtil.getBytes(file);
				if (byteInFile != null){
					supDoc.setDocumentUpload(Base64.encode(byteInFile));
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			 
			
//			String fullFileName = uploadRequest.getFullFileName("fileName_" + i);
//			String description = ParamUtil.getString(
//	                uploadRequest, "description", fileName);
//	

			
			String contentType = uploadRequest.getContentType("fileName_" + i);
			
			supDoc.setDocumentType(contentType);//this will change
			
			

			ThemeDisplay themeDisplay = (ThemeDisplay) request
			.getAttribute(WebKeys.THEME_DISPLAY);
			
			supDoc.setUserId(new Long(themeDisplay.getUserId()).toString());
			
			supDoc.setUploadedDate(CalendarFactoryUtil.getCalendar().getTime());
			
			suppDocs.add(supDoc);
			
		}
		
		//String[] docIds =  ParamUtil.get
		return suppDocs;
	}
	
		
	
	/**
	 * This is the temporary implementation of the mapping of application 
	 * type to the supporting document. This will change when we clearly 
	 * know how we would want this to be implemented.
	 * 
	 * @param request
	 * @return List<Supporting_Document> 
	 */
	public static  List<Supporting_Document> 
			getSupporting_DocumentInput(Application_C_of_O applCofO){
				
		log.info("In  pickApplicationType " );
		
		
		String [] documentsIndividual = new String[]{
				"Certified Bank Draft from any bank",
				"Tax Clearance Certificate",
				"Passport sized Photograph",
				"Copy of National ID Card",
				"Copy of International Passport",
				"Copy of Birth Certificate / Age Declaration",
				"Copy of C-of-O / R-of-O for previous Allocations",
				"Payment Slip / Receipt from Development Control"				
				};
		
		
		String [] documentsOrganisationCommerical = new String[]{
			"Certified Bank Draft from any bank",
			"Tax Clearance Certificate",
			"Registration/Particulars Of Director",
			"Certificate Of Incorporation",
			"Memorandum Of Article Of Association",
			"Schematic Design/Project Feasibility Studies",
			"Environmental Impact Analysis Report",
			"Copy Of C-of-O/ R-of-O for Previous Application"				
		};
		
		String [] documentsOrganisationInst = new String[]{
			"Certified Bank Draft from any bank",
			"Tax Clearance Certificate",
			"Registration/Particulars Of Director",
			"Certificate Of Incorporation",
			"Memorandum Of Article Of Association",
			"Schematic Design/Project Feasibility Studies",
			"Copy Of C-of-O/ R-of-O for Previous Application"				
		};
		
		String [] documentsOrganisationNGO = new String[]{
			"Certified Bank Draft from any bank",
			"Tax Clearance Certificate",
			"Registration/Particulars Of Director",
			"Memorandum Of Article Of Association",
			"Schematic Design/Project Feasibility Studies",
			"Copy Of C-of-O/ R-of-O for Previous Application"				
		};
		
		String [] documentsOrganisationGovt = new String[]{
			"Certified Bank Draft from any bank",
			"Letter Of Recommendation",
			"Copy Of C-of-O/ R-of-O for Previous Application"				
		};
		
		String [] documentsOrganisationDipl = new String[]{
				"Certified Bank Draft from any bank",
				"Letter Of Recommendation",
				"Copy Of C-of-O/ R-of-O for Previous Application"					
		};
		
		String [] documentsOrganisationMassHousing = new String[]{
			"Certified Bank Draft from any bank",
			"Certificate Of Incorporation",
			"Memorandum Of Article Of Association",
			"Schematic Design/Project Feasibility Studies",
			"Environmental Impact Analysis Report",
			"Copy Of C-of-O/ R-of-O for Previous Application"	,
			"Payment Slip/Draft for PPA"			
		};
		
		
		String appType = applCofO.getApplicationType();
		if(null == appType || "".equalsIgnoreCase(appType)){
			appType = 
				ApplicationCofOConstants.APPLICATION_TYPE_INDIVIDUAL;
		}
		List<Supporting_Document> supDocs= new ArrayList<Supporting_Document>();
		if(ApplicationCofOConstants.APPLICATION_TYPE_INDIVIDUAL.equalsIgnoreCase(appType)){
			return buildSupportDocs(documentsIndividual, applCofO.getApplicationId());
		}else{
			
			if (applCofO.getLandUseType().equalsIgnoreCase("Commerical")){
				return buildSupportDocs(documentsOrganisationCommerical, applCofO.getApplicationId());
			}else if(applCofO.getLandUseType().equalsIgnoreCase("Inst")){
				return buildSupportDocs(documentsOrganisationInst, applCofO.getApplicationId());
			}else if(applCofO.getLandUseType().equalsIgnoreCase("NGO")){
				return buildSupportDocs(documentsOrganisationNGO, applCofO.getApplicationId());
			}else if(applCofO.getLandUseType().equalsIgnoreCase("Govt")){
				return buildSupportDocs(documentsOrganisationGovt, applCofO.getApplicationId());
			}else if(applCofO.getLandUseType().equalsIgnoreCase("Dipl")){
				return buildSupportDocs(documentsOrganisationDipl, applCofO.getApplicationId());
			}else if(applCofO.getLandUseType().equalsIgnoreCase("MassHousing")){
				return buildSupportDocs(documentsOrganisationMassHousing, applCofO.getApplicationId());
			}else{
				return buildSupportDocs(documentsOrganisationCommerical, applCofO.getApplicationId());
			}
			
		}
		
		//case application organozation but no land use type
		
		//return null;
	}
	
	/**
	 * This method is used for building list of Supporting_Document information
	 * from the ActionRequest object from the portal.
	 * 
	 * @param request
	 * @return list of Supporting_Document
	 */
	static protected List<Supporting_Document> buildSupportDocs(String[] docs, String applicationId){
	
		List<Supporting_Document> supDocs= new ArrayList<Supporting_Document>();
		
		for (int i = 0; i < docs.length; i++) {
			Supporting_Document suppDoc = new Supporting_DocumentImpl();
			suppDoc.setApplicationId(applicationId);
			suppDoc.setDocumentName(docs[i]);
			suppDoc.setDocumentId(UUID.randomUUID().toString());
			supDocs.add(suppDoc);
		}
		
		return supDocs;
	}

	public static Organisation_Information getOrganisation_InformationFromRequest(
			ActionRequest request) {
		
		PortletSession portletSession = request.getPortletSession();
		Organisation_Information  organisation  = (Organisation_Information)
			portletSession.getAttribute(ApplicationCofOConstants.ORGANISATION_INFO_SESSION_ATTR);
		
		if(organisation == null){
			organisation = new Organisation_InformationImpl();
		}
		
		ThemeDisplay themeDisplay = (ThemeDisplay) request
			.getAttribute(WebKeys.THEME_DISPLAY);
		
		
		
		organisation.setApplicationId(ParamUtil.getString(request, "applicationId"));
		organisation.setUserId(new Long(themeDisplay.getUserId()).toString());
		
		organisation.setCountryOfReg(ParamUtil.getString(request, "countryOfReg"));
		organisation.setNatureOfBusiness(ParamUtil.getString(request, "natureOfBusiness")); 
		organisation.setRegNo(ParamUtil.getString(request, "regNo"));
		organisation.setOrganisationName(ParamUtil.getString(request, "organisationName"));
		
		Calendar cal = null;
//		CalendarFactoryUtil.getCalendar(ParamUtil.getInteger(request, "dateOfRegYear"), 
//				ParamUtil.getInteger(request, "dateOfBirthMonth"), 
//					ParamUtil.getInteger(request, "dateOfBirthDay"));
		
		if(ParamUtil.getInteger(request, "dateOfRegYear") == 0 && 
				ParamUtil.getInteger(request, "dateOfRegMonth") == 0 && 
				ParamUtil.getInteger(request, "dateOfRegDay")== 0){
			
			cal = 	CalendarFactoryUtil.getCalendar();
			
		}else{
			cal = CalendarFactoryUtil.getCalendar(ParamUtil.getInteger(request, "dateOfRegYear"), 
					ParamUtil.getInteger(request, "dateOfRegMonth"), 
						ParamUtil.getInteger(request, "dateOfRegDay"));
		}
		
		organisation.setDateOfRegistration(cal.getTime());
		
		return organisation;
	}
}
